/*
 * Multiport Web Server
 *
 * A simple web server that shows
 * the value of the analog and digital input pins.
 *
 * port 80: human readable page
 * port 8080: machine readable page (csv format)
 */
#include <OneWire.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 123 };
//byte gateway[] = { 192, 168, 0, 254 };
//byte subnet[] = { 255, 255, 255, 0 };

Server server1(80); // server for human beings
Server server2(8080); // server for machines
// DS18S20 Temperature chip i/o

OneWire ds(9);  // on pin 10

void setup()
{
  for (int i = 2 ; i < 8; i++){
    pinMode(i, INPUT);
    //digitalWrite(i, HIGH);
  }
  //Ethernet.begin(mac, ip, gateway, subnet);
  Ethernet.begin(mac, ip);
  server1.begin();
  server2.begin();
}

void loop()
{
  byte i;
  byte present = 0;
  byte data[12];
  byte addr[8];


  Client client1 = server1.available();
  Client client2 = server2.available();
  if (client1) {
    boolean current_line_is_blank = true;
    while (client1.connected()) {
	if (client1.available()) {
	  char c = client1.read();
	  if (c == '\n' && current_line_is_blank) {
	    client1.println("HTTP/1.1 200 OK");
	    client1.println("Content-Type: text/html");
	    client1.println();

	    
//            if ( !ds.search(addr)) {
//              // No more adresses
//              ds.reset_search();
//            }
            
            //else {
            while (ds.search(addr)) {
              client1.print("A=");
              for( i = 0; i < 8; i++) {
                client1.print(addr[i], HEX);
                client1.print(" ");
              }

              if ( OneWire::crc8( addr, 7) != addr[7]) {
                client1.print("CRC is not valid!\n");
                client1.println("<br />");

                //      return;
              }

              if ( addr[0] != 0x28) {
                client1.print("Device is not a DS18S20 family device.\n");
                client1.println("<br />");

                //      return;
              }

              ds.reset();
              ds.select(addr);
              ds.write(0x44,1);         // start conversion, with parasite power on at the end

              delay(1000);     // maybe 750ms is enough, maybe not
              // we might do a ds.depower() here, but the reset will take care of it.

              present = ds.reset();
              ds.select(addr);    
              ds.write(0xBE);         // Read Scratchpad

              for ( i = 0; i < 9; i++) {           // we need 9 bytes
                data[i] = ds.read();
              }
              int rawtemp = (data[1] << 8) + data[0];
              int tempca = rawtemp / 16.0;
              int tempcb = rawtemp % 16;

              client1.print(" Celcius = ");
              client1.print(tempca);
              client1.print(".");
              client1.print(tempcb);
              client1.println("<br />");
            }
            // No more device then reset the search
            ds.reset_search();




/*
	    for (int i = 0; i < 6; i++) {
		client1.print("analog input ");
		client1.print(i);
		client1.print(" is ");
		client1.print(analogRead(i));
		client1.println("<br />");
	    }
	    for (int i = 2; i < 8; i++) {
		client1.print("digital input ");
		client1.print(i);
		client1.print(" is ");
		if (digitalRead(i)) {
		  client1.print("HIGH");
		} else {
		  client1.print("LOW");
		}
		client1.println("<br />");
	    }
*/
	    break;
	  }
	  if (c == '\n') {
	    current_line_is_blank = true;
	  } else if (c != '\r') {
	    current_line_is_blank = false;
	  }

	}
    }
  
    delay(10);
    client1.stop();
  }
  
  if (client2) {
    boolean current_line_is_blank = true;
    while (client2.connected()) {
	if (client2.available()) {
	  char c = client2.read();
	  if (c == '\n' && current_line_is_blank) {
	    client2.println("HTTP/1.1 200 OK");
	    client2.println("Content-Type: text/html");
	    client2.println();
	    for (int i = 0; i < 6; i++) {
		if (i != 0)
		  client2.print(",");
		client2.print(analogRead(i));
	    }
	    for (int i = 2; i < 8; i++) {
		client2.print(",");
		client2.print(digitalRead(i));
	    }
	    break;
	  }
	  if (c == '\n') {
	    current_line_is_blank = true;
	  } else if (c != '\r') {
	    current_line_is_blank = false;
	  }
	}
    }
    delay(10);
    client2.stop();
  }

} 








